import
BASE_URL
from './env.js'
// 获取token
export const getToken = () => uni.getStorageSync('token')
// 设置token
export const setToken = data => uni.setStorageSync('token', data)
// 清除token
export const removeToken = () => uni.removeStorageSync('token')
// 获取用户信息
export const getUserInfo = (key = '') => {
	const userInfo = uni.getStorageSync('userInfo')
	if (userInfo) {
		return '' === key ? userInfo : userInfo[key];
	}
	return '';
}
// 设置用户信息
export const setUserInfo = data => uni.setStorageSync('userInfo', data)
// 地址拼接
export const imgPrefix = (url) => {

	if (!url) {
		return url
	}
	// 如果传入的参数是一个字符串
	try {
		if (url.startsWith('https')) {
			return url
		} else {
			return BASE_URL + '/' + url
		}
	} catch (e) {
		return url
	}
}
// 将对象和字符串相互转换
export const objToJson = obj => encodeURLComponent('page_data', obj)
export const jsonToObj = () => uni.getStorageSync('page_data')

// 将对象存入缓存
export const setPageCache = (name, obj) => {
	uni.setStorageSync(name, obj)
}

// 将对象取出并且删除缓存
export const getPageCache = name => {
	const cache = uni.getStorageSync(name)
	uni.removeStorageSync(name)
	return cache
}
// 将数据存入缓存
function setValue(key, value) {
	uni.setStorageSync(key, value);
}
// 将数据从缓存中取出
function getValue(key, default_value = '') {
	return uni.getStorageSync(key) || default_value || ''
}

function removeValue(key) {

	return uni.removeStorageSync(key)

}
// 判断是否登录
function isLogin() {
	return !!getToken();
}
const loading = (title = '加载中', mask = true) => uni.showLoading({
	title: title,
	mask: mask
})
const hideLoading = () => uni.hideLoading()

// 信息提示
function msg(title, duration = 1000) {
	uni.showToast({
		title: title,
		duration: duration,
		icon: 'none',
		mask: true,
	});
}
// 弹出框提示
function show(title, msg, showCancel = true, confirmText = '确定') {
	let res = new Promise((resolve, reject) => {
		uni.showModal({
			title: title,
			content: msg,
			confirmText: confirmText,
			showCancel: showCancel,
			success: function(res) {
				resolve(res.confirm)
			},
			fail: function(res) {
				reject(false)
			},
		});
	})
	return res;
}
// 微信登录
function login() {
	let res = new Promise((resolve, reject) => {
		uni.login({
			"provider": "weixin",
			"onlyAuthorize": true, // 微信登录仅请求授权认证
			success: function(event) {
				const {
					code
				} = event
				console.log(code, "code=--22")



			},
			fail: function(err) {
				// 登录授权失败
				// err.code是错误码
				reject(false)
			}
		})

	})
	return res;
}
// 微信用户信息
function getUserInfoUni() {
	let res = new Promise((resolve, reject) => {
		uni.getUserInfo({
			desc: '用于获取用户信息', // 确保描述准确且符合要求
			success: (res) => {
				console.log('用户信息获取成功', res.userInfo);
				setUserInfo(res.userInfo)
				resolve(res.userInfo)
			},
			fail: (err) => {
				console.error('用户信息获取失败', err);
				reject(false)
			}
		});

	})
	return res;
}
// 保留当前页面，跳转到应用内的某个页面，使用uni.navigateBack可以返回到原页面。
const navigateTo = url => uni.navigateTo({
	url: url
});
// 关闭所有页面，打开到应用内的某个页面。
const reLaunch = url => {
	uni.reLaunch({
		url: url
	});
}
// 跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面。
const switchTab = url => {
	uni.switchTab({
		url: url
	});
}
// 关闭当前页面，返回上一页面或多级页面。可通过 getCurrentPages() 获取当前的页面栈，决定需要返回几层。
const navigateBack = (delta = 1, fun = () => {}) => {
	uni.navigateBack({
		delta: delta,
		success() {
			fun()
		}
	})
}

//关闭当前页面，跳转到应用内的某个页面。
const redirectTo = url => uni.redirectTo({
	url: url
});

// 时间转换时间戳
const dateToTime = (dateString) => {
	let date = new Date(dateString);
	let timestamp = date.getTime();
	return timestamp
}

// 数字格式化
const formatDate2 = (timestamp, format = 'yyyy-MM-dd HH:mm:ss') => {
	if (!timestamp)
		return ''
	let date
	try {
		if (timestamp) {
			if (typeof timestamp == 'number' && timestamp <= 9999999999) {
				timestamp *= 1000
			}
			date = new Date(timestamp)
		} else {
			date = new Date()
		}
	} catch (e) {
		return ''
	}
	let day = date.getDate()
	let month = date.getMonth() + 1
	let year = date.getFullYear()
	let hours = date.getHours()
	let minutes = date.getMinutes()
	let seconds = date.getSeconds()

	// 替换日期格式中的占位符
	return format
		.replace('yyyy', year)
		.replace('MM', month.toString().padStart(2, '0'))
		.replace('dd', day.toString().padStart(2, '0'))
		.replace('HH', hours.toString().padStart(2, '0'))
		.replace('mm', minutes.toString().padStart(2, '0'))
		.replace('ss', seconds.toString().padStart(2, '0'))
}
export default {
	getPageCache,
	setPageCache,
	objToJson,
	jsonToObj,
	hideLoading,
	loading,
	msg,
	show,
	redirectTo,
	navigateTo,
	navigateBack,
	formatDate2,
	imgPrefix,
	setValue,
	getValue,
	removeValue,
	isLogin,
	reLaunch,
	switchTab,
	dateToTime,
	login,
	getUserInfoUni
}